package com.sgb.kjwl.viewmodel.manage;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.jicai.entity.FactoryCentralizedListEntity;
import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.sgb.kjwl.view.ui.fragment.manage.JiCaiManageFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.Map;

/**
 * Time:2021/5/16
 * AUTHOR:F_Q
 * Description:
 */
public class JiCaiManageModel extends BaseViewModel {
    private JiCaiManageFragment fragment;
    private int current;

    public JiCaiManageModel(JiCaiManageFragment fragment) {
        this.fragment = fragment;
    }

    //企业网站->经营业务->集采经营->供方发起列表
    public void getGongFangFaQiList(String websiteNo, boolean isLoadMore) {
        Map map = new HashMap<>();
        int currentPage = isLoadMore ? current + 1 : 1;
        map.put("current",currentPage);
        map.put("limit",10);
        map.put("companyNo",websiteNo);
        if (!DialogHelper.isShowing()) {
            DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        }
        APIManager.getInstance().getCentralizedMiningAPI().getFactoryCentralizedList(map).enqueue(new ICallBack<BaseEntity<FactoryCentralizedListEntity<FactoryCentralizedItemEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<FactoryCentralizedListEntity<FactoryCentralizedItemEntity>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getTotal();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore,current >= total);
            }
        });
    }

    //加载详情
    public void getH5Url(String no) {
            DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
            APIManager.getInstance().getPickAPI().getUrlSetting("Factory").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
                @Override
                public void onSuccess(BaseEntity<H5ReEntity> data) {
                    DialogHelper.dismissProgressDialog();
                    if (data.getState().equals(APIManager.RESPONSE_OK)) {
                        String url = data.getData().getUrl() + "?id=" + no;
                        fragment.showUrl(url);
                    } else {
                        MToast.showToast(fragment.getContext(), data.getMsg());
                    }
                }
            });
    }
}
